package com.gadgets.framework.task;

import com.gadgets.framework.common.dto.WoodenGeneratorCommandDTO;
import com.gadgets.framework.service.IWoodenProductService;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

/**
 * 功能说明：令牌生成器
 *
 * @author Unclexiu
 * @version V202101.00.000
 * @data 2022-07-18
 * 修改记录：  修改日期          修改人员    修改说明
 * 2022-07-18  xiujunqing   创建文件
 */
@Component
public class WoodenGeneratorTask extends QuartzJobBean {

	@Autowired
	@Qualifier("woodenPriductServiceImpl")
	private IWoodenProductService woodenProdCommandTrigger;

	private static final List<WoodenGeneratorCommandDTO> commandDTOList = new ArrayList<>();

	/**
	 * Execute the actual job. The job data map will already have been
	 * applied as bean property values by execute. The contract is
	 * exactly the same as for the standard Quartz execute method.
	 *
	 * @param context	Spring容器上下文
	 * @see #execute
	 */
	@Override
	protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
		commandDTOList.sort(Comparator.comparingInt(WoodenGeneratorCommandDTO::getOrder));
		for (WoodenGeneratorCommandDTO commandDTO : commandDTOList) {
			woodenProdCommandTrigger.productWoodenForModuel(
					commandDTO.getModuelNo(),
					commandDTO.getMaxNum(),
					commandDTO.getNum()
			);
		}
	}

	public void addWoodenCommandInfo(WoodenGeneratorCommandDTO dto){
		commandDTOList.add(dto);
	}

}
